package com.zixun.service.Impl;

import com.zixun.model.request.Request;
import com.zixun.model.request.RequestComment;
import com.zixun.model.request.RequestImage;
import com.zixun.repository.request.RequestCommentRepository;
import com.zixun.repository.request.RequestImageRespository;
import com.zixun.repository.request.RequestRepository;
import com.zixun.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Basil on 16/8/23.
 */
@Service
public class RequestServiceImpl implements RequestService {
    @Autowired
    RequestRepository requestRepository;

    @Autowired
    RequestCommentRepository requestCommentRepository;

    @Autowired
    RequestImageRespository requestImageRespository;
    //查询指定用户编号的提问列表
    @Override
    public Page<Request> findAllByUserIdRequest(final Integer userId, Integer pageIndex, Integer countPerPage) {
        Specification<Request> requestSpecification =new Specification<Request>() {
            @Override
            public Predicate toPredicate(Root<Request> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Integer> $userId = root.get("userId");
                Predicate _userId = cb.equal($userId,userId);
                return cb.and(_userId);
            }
        };
        PageRequest pageable =new PageRequest(pageIndex-1,countPerPage);
        return requestRepository.findAll(requestSpecification,pageable);
    }

    //查询指定用户编号的回答列表
    @Override
    public Page<RequestComment> findAllByReplyUserIdRequest(final Integer replyUserId, Integer pageIndex, Integer countPerPage) {
        Specification<RequestComment> requestCommentSpecification =new Specification<RequestComment>() {
            @Override
            public Predicate toPredicate(Root<RequestComment> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Integer> $replyUserId  =root.get("replyUserId");
                Predicate _replyUserId =cb.equal($replyUserId,replyUserId);
                return cb.and(_replyUserId);
            }
        };
        Pageable pageable =new PageRequest(pageIndex-1,countPerPage);
        return requestCommentRepository.findAll(requestCommentSpecification,pageable);
    }

    @Override
    public Page<Request> findAllByUserIdAndTypeRequest(final Integer userId, Integer pageIndex, Integer countPerPage, final Integer type) {
        Specification<Request> requestSpecification =new Specification<Request>() {
            @Override
            public Predicate toPredicate(Root<Request> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                Path<Integer> $type =root.get("type");
                Predicate _type =cb.equal($type,type);
                predicates.add(_type);
                return cb.and(predicates
                        .toArray(new Predicate[] {}));
            }
        };
        Pageable pageable =new PageRequest(pageIndex-1,countPerPage);
        return requestRepository.findAll(requestSpecification,pageable);
    }

    @Override
    public Request findByIdAndType(Integer id, Integer type) {
        return requestRepository.findByIdAndType(id,type);
    }

    @Override
    public List<RequestImage> findByRequestId(Integer id) {
        return requestImageRespository.findByRequestId(id);
    }

    @Override
    public List<RequestComment> findByRequestIdCom(Integer id) {
        return requestCommentRepository.findByRequestId(id);
    }
}
